#!/bin/bash

set -ux

# doccker root  dir
docker_root=/home/docker/data
docker_compose_version=v2.32.2

# 辅助函数：检查命令执行结果并输出日志
check_command_status() {
  if [ $? -ne 0 ]; then
    echo "命令执行失败: $1"
    exit 1
  fi
}

#/ sshd配置，禁用DNS反向解析和GSSAPI认证
sshdSet() {
  echo '修改ssh配置'
  sed -i -e '/^#UseDNS/c UseDNS no' \
    -e '/^GSSAPIAuthentication/c GSSAPIAuthentication no' \
    /etc/ssh/sshd_config

  systemctl restart sshd
  check_command_status "重启 sshd 服务"
}

# 安装docker 8版本
dockerInstall_V8() {
  # 卸载就版本 docker
  dnf remove docker \
    docker-client \
    docker-client-latest \
    docker-common \
    docker-latest \
    docker-latest-logrotate \
    docker-logrotate \
    docker-engine &&
    dnf clean all

  # 添加docker 源
  curl -o /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

  # 安装docker
  dnf install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y &&
    dnf clean all
  check_command_status "安装docker"
}

# 安装docker 23版本
dockerInstall_V23() {
  # 安装docker
  dnf install docker -y &&
    dnf clean all
  check_command_status "安装docker"
}

# 安装docker
dockerInstall() {
  os_release=/etc/os-release
  # 判断版本
  if [ -f $os_release ]; then
    # 获取版本号
    version=$(grep "VERSION=" $os_release | cut -d'"' -f2)

    # 比较版本号是否小于23
    if printf '%s\n' "$version" '23' | sort -V | head -n1 | grep -q '23'; then
      echo "版本号不小于23"
      dockerInstall_V23
    else
      echo "版本号小于23"
      dockerInstall_V8
    fi
  fi

  # 设置开机自启
  systemctl enable docker &&
    systemctl start docker
  check_command_status "docker开机自启和重启docker服务"
}

# 设置docker配置文件
dockerConfig() {
  # 设置docker
  mkdir -p /etc/docker
  cat >/etc/docker/daemon.json <<-EOF
{
  "registry-mirrors": [
    "https://3otatncp.mirror.aliyuncs.com",
    "https://ac8965cb96a04f8e864a71203f1e5f2c.mirror.swr.myhuaweicloud.com/",
    "https://dockerpull.org",
    "https://docker.1panel.dev",
    "https://docker.foreverlink.love",
    "https://docker.fxxk.dedyn.io",
    "https://docker.xn--6oq72ry9d5zx.cn",
    "https://docker.zhai.cm",
    "https://docker.5z5f.com",
    "https://a.ussh.net",
    "https://docker.cloudlayer.icu",
    "https://hub.littlediary.cn",
    "https://hub.crdz.gq",
    "https://docker.unsee.tech",
    "https://docker.kejilion.pro",
    "https://registry.dockermirror.com",
    "https://hub.rat.dev",
    "https://dhub.kubesre.xyz",
    "https://docker.nastool.de",
    "https://docker.udayun.com",
    "https://docker.rainbond.cc",
    "https://hub.geekery.cn",
    "https://docker.1panelproxy.com",
    "https://atomhub.openatom.cn",
    "https://docker.m.daocloud.io",
    "https://docker.1ms.run",
    "https://docker.linkedbus.com",
    "https://dytt.online",
    "https://func.ink",
    "https://lispy.org",
    "https://docker.xiaogenban1993.com"
  ],
  "insecure-registries":[
    "https://registry.glacier.top:6000"
  ],
  "data-root": "$docker_root",
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "10"
  }
}
EOF

  # 开启远程管理
  mkdir -p /etc/systemd/system/docker.service.d
  cat >/etc/systemd/system/docker.service.d/override.conf <<EOF
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375
EOF

  # 重启生效
  systemctl daemon-reload &&
    systemctl restart docker
  check_command_status "docker配置重启docker服务"

  # 开启端口转发
  echo "net.ipv4.ip_forward = 1" >>/etc/sysctl.conf &&
    sysctl -p

  # 防火墙放行IP地址转发
  firewall-cmd --add-masquerade --permanent &&
    firewall-cmd --reload

  # 开放远程管理端口
  firewall-cmd --zone=public --add-port=2375/tcp --permanent &&
    firewall-cmd --zone=public --add-port=2377/tcp --permanent &&
    firewall-cmd --reload
}

# 安装docker-compose
dockerComposeInstall() {
  # 安装docker-compose
  curl -Lo docker-compose https://github.moeyy.xyz/https://github.com/docker/compose/releases/download/$docker_compose_version/docker-compose-$(uname -s)-$(uname -m) &&
    rm -rf /usr/local/bin/docker-compose &&
    mv docker-compose /usr/local/bin/docker-compose &&
    chmod +x /usr/local/bin/docker-compose
}

# 查看信息
showInfo() {
  # 查看开放的端口
  firewall-cmd --zone=public --list-ports

  # 查看容器
  docker info

  # 查看docker-compose
  docker-compose version
}

sshdSet
dockerInstall
dockerConfig
dockerComposeInstall
showInfo
